/*
 *  Copyright 2010 Daniele.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package util.acordionmenu;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;

/**
 * AccordionBranch is a simple <code>JPanel</code> object. It will be used to
 * display menu items under a Menu Root Item. It is an inheritance of JPanel because
 * first of all it has a different layout, like a little margin around future menu
 * items. It also implements a cheap method to fill void spaces when there is a small
 * number of menu items, avoiding bad displaying. However it is a simple handle that
 * binds a MenuRootItem with his leafs. Here will be implemented a general container
 * for everything like JButton, JChekBox...
 *
 * This class is created with NetBeans Visual Editor so it has a <code>initComponents()</code>
 * method auto-generated by IDE.
 * 
 * @author Daniele
 */
public class AccordionBranch extends javax.swing.JPanel {

    /** Number of items in branch. */
    private int count = 0;
    /** Fake void elements used to fill blank spaces.*/
    private List<JLabel> fooItems;

    /**
     * Default constructor. Sets transparent container
     */
    public AccordionBranch() {
        initComponents();
        container.setOpaque(false);
        container.setLayout(new GridLayout(0, 1, 0,5));
        this.fooItems = new ArrayList<JLabel>();
    }

    /**
     * Add an {@link AccordionItem} to this branch panel.
     * @param item Target item to insert in.
     */
    public void addItem(AccordionItem item) {
        container.add(item);
        count++;
    }

    /**
     * By a reference number it fills blank space with void <code>JLabel</code>s
     * @param max Number of desidered elements. It creates (<code>max - count</code>) fake elements.
     */
    public void adjustItems(int max) {
        for (JLabel l : this.fooItems) {
            container.remove(l);
        }
        this.fooItems.clear();
        for (int i = max; i > getCount(); i--) {
            JLabel lab = new JLabel("");
            lab.setBackground(Color.green);
            container.add(lab);
            this.fooItems.add(lab);
        }
    }

    /**
     *
     * @return Number of items of branch.
     */
    public int getCount() {
        return count;
    }

    /**
     * Automatic Layout code generation with NETBEANS. It uses a GroupLayout for
     * a panel with Margin around. It is more simple do that with a visual editor like
     * netbeans one that writing code from blank page.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        container = new javax.swing.JPanel();

        container.setLayout(new java.awt.GridLayout(0, 1, 0, 5));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(container, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(container, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel container;
    // End of variables declaration//GEN-END:variables
}
